const md5 = require("md5");
const jsonwebtoken = require('jsonwebtoken');
const secretKey = '3.14159263528542852651268541';
class UserController {
    async login(ctx) {  //登录
        const { pass, name } = ctx.request.body;
        const data = await ctx.app.Service.User.findOne({
            where: {
                username: name,
                password: md5(pass),
            },
            attributes: {
                exclude: ['password'] //排除此字段返回其余字段
            },
        });
        if (data != null) {
            await ctx.app.Service.User.update({ last_logined_at: new Date().getTime() }, {
                where: {
                    username: name,
                    password: md5(pass),
                }
            })
            const authorization = await jsonwebtoken.sign(data.dataValues, secretKey, { expiresIn: '7d' });
            ctx.body = {
                code: 200,
                message: '登录成功！',
                authorization,
            }
        } else {
            ctx.status = 202;
            ctx.body = {
                code: 202,
                message: '用户名或密码错误！'
            }
        }
    }
    async register(ctx) {   //注册
        const { pass, name } = ctx.request.body;
        const date = new Date();
        const data = await ctx.app.Service.User.findOrCreate({
            where: { username: name, },
            defaults: {
                origin_id: md5(date.getTime() + Math.random()),
                username: name,
                password: md5(pass),
                created_at: date.getTime(),
                last_logined_at: date.getTime()
            },
        });
        if (data[data.length - 1]) {
            ctx.body = {
                code: 200,
                message: '注册成功！'
            }
        } else {
            ctx.status = 202;
            ctx.body = {
                code: 202,
                message: '用户名被占用'
            }
        }
    }
    async getUserToken(ctx) {   //通过token获取用户信息
        const data = await ctx.app.Service.User.findOne({
            where: {
                username: ctx.state.user.username,
            },
            attributes: {
                exclude: ['password'] //排除此字段返回其余字段
            },
        })
        ctx.body = data;
    }
    async getUser(ctx) {   //通过username获取用户信息
        const { username = "" } = ctx.query;
        const data = await ctx.app.Service.User.findOne({
            where: { username },
            attributes: {
                exclude: ['password'] //排除此字段返回其余字段
            },
        })
        ctx.body = data;
    }
    async ceshi(ctx) {
        const data = await ctx.app.Service.User.count({
            group: 'username',
            attributes: ['id', 'username']
        })
        let arr = [];
        for (let i = 0; i < data.length; i++) {
            if (data[i].count != 1) {
                await ctx.app.Service.User.update(
                    {
                        username: data[i].username + '1'
                    },
                    {
                        where: {
                            id: data[i].id
                        }
                    }
                )
                arr.push(data[i])
            }
        }
        ctx.body = arr;
    }
}
module.exports = UserController;
